package com.example.soa2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.soa2.entity.Identity;
import com.example.soa2.entity.UserDTO;
import com.example.soa2.entity.UserProfile;
import com.example.soa2.persistence.IdentityMapper;
import com.example.soa2.persistence.UserProfileMapper;
import com.example.soa2.service.UserService;
import com.example.soa2.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserProfileMapper userProfileMapper;

    @Autowired
    private IdentityMapper identityMapper;

    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<UserVO> getAllUsers() {
        List<UserProfile> profiles = userProfileMapper.selectList(null);
        List<Identity> identities = identityMapper.selectList(null);
        List<UserVO> users = new ArrayList<>();
        for (UserProfile profile : profiles) {
            UserVO vo;
            Identity identity = identities.get(Integer.parseInt(profile.getIdentity())-1);
            vo = merge(profile,identity);
            users.add(vo);
        }
        return users;
    }

    @Override
    public UserVO getUserById(String id) {
        List<Identity> identities = identityMapper.selectList(null);
        UserProfile profile = userProfileMapper.selectById(id);
        if(profile!=null){
            return merge(profile,identities.get(Integer.parseInt(profile.getIdentity())-1));
        }else
            return null;
    }

    @Override
    public UserVO getUserByName(String name) {
        List<Identity> identities = identityMapper.selectList(null);
        QueryWrapper<UserProfile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        UserProfile pro = userProfileMapper.selectOne(queryWrapper);
        if(pro!=null){
            return merge(pro,identities.get(Integer.parseInt(pro.getIdentity())-1));
        }
        return null;
    }

    @Override
    public int addUser(UserDTO dto) {
        UserProfile userProfile = transformDTOtoProfile(dto);
        Date date = new Date(System.currentTimeMillis());
        userProfile.setCreate_time(formatter.format(date));
        return userProfileMapper.insert(userProfile);
    }

    @Override
    public int deleteUserByName(String name) {
        QueryWrapper<UserProfile>   queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return userProfileMapper.delete(queryWrapper);
    }

    @Override
    public ArrayList<Integer> deleteUsersByIdList(String[] ids) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (String id:ids) {
            arrayList.add(userProfileMapper.deleteUserProfile(id));
        }
        return arrayList;
    }

    @Override
    public int updateUser(UserDTO dto) {
        QueryWrapper<UserProfile>   queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",dto.getName());
        return userProfileMapper.update(transformDTOtoProfile(dto),queryWrapper);
    }

    private UserProfile transformDTOtoProfile(UserDTO dto){
        UserProfile vo = new UserProfile();
        vo.setTelephone(dto.getTelephone());
        vo.setName(dto.getName());
        vo.setSex(dto.getSex());
        vo.setAge(dto.getAge());
        vo.setIdentity(dto.getIdentity());
        vo.setEmail(dto.getEmail());
        Date date = new Date(System.currentTimeMillis());
        vo.setUpdate_time(formatter.format(date));
        return vo;
    }

    private UserVO merge(UserProfile userProfile, Identity identity){
        UserVO vo = new UserVO();
        vo.setId(userProfile.getId());
        vo.setTelephone(userProfile.getTelephone());
        vo.setName(userProfile.getName());
        vo.setSex(userProfile.getSex());
        vo.setAge(userProfile.getAge());
        vo.setIdentity(identity.getIdentity());
        vo.setEmail(userProfile.getEmail());
        vo.setCreate_time(userProfile.getCreate_time());
        vo.setUpdate_time(userProfile.getUpdate_time());
        return vo;
    }


}
